#include "blueTeeth.h"
#include "dicar.h"
#define LED_PIN 7
#define LED_BREATH_PIN 3
#define BEEP_PIN 3
#define T1  262
#define T2  294
#define T3  330
#define T4  349
#define T5 392
#define T6  440
#define T7  494


int music_tone[] = {
  T1, T2, T3, T1, T1, T2, T3, T1,
  T3, T4, T5, T3, T4, T5,
  T5, T6, T5, T4, T3, T1, 
  T5, T6, T5, T4, T3, T1, 
  T1, T5, T1, T1, T5, T1
  };
  int tone_time[] = {
  4, 4, 4,4, 4, 4, 4, 4,
  4, 4, 8, 4, 4, 8,
  2, 1, 2, 1, 4, 4 ,
  2, 1, 2, 1, 4, 4 , 
  4,4, 8, 4, 4, 8
  };
void led_init(int led_pin)
{
  pinMode(led_pin, OUTPUT);
  }
  void led_on(int led_pin)
  {
    digitalWrite(led_pin, LOW);
    }
     void led_off(int led_pin)
  {
    digitalWrite(led_pin, HIGH);
    }
    void led_breath(int led_pin)
    {
      int i = 0;
      while(1)
      {
        for(i = 0; i <= 255; i++)
        {
      analogWrite(led_pin,i);
      delay(5);
        }
        for(i = 256; i>=0; i--)
        {
          analogWrite(led_pin,i);
            delay(5);
          }
       }

    }
    void beep_init(int beep_pin)
    {
      pinMode(beep_pin,OUTPUT);
      }
    void beep_on(int beep_pin)
    {
      tone(BEEP_PIN, 500);
      delay(1000);
      noTone(BEEP_PIN);
    }
      void play_music()
     {
      int i = 0;
      for(i = 0; i<sizeof(music_tone)/sizeof(int);i++)
      {
        tone(BEEP_PIN, music_tone[i]);
        delay(50 * tone_time[i]);
        noTone(BEEP_PIN);
        
        }
      }
void setup() {
  // put your setup code here, to run once:
Serial. begin(9600);
Serial. println("hello word\n");
blueTeeth_init();
led_init( LED_PIN);
beep_init(BEEP_PIN);
beep_on(BEEP_PIN);
play_music();
//led_breath(LED_BREATH_PIN);
}

void loop() {
  // put your main code here, to run repeatedly:
//Serial. println("hqyj\n");
int bytes = 0;
char ch = 0;
//判断是否有可读的串口数据
//1bytes = Serial. available();
bytes = blueTeeth.available();
if  (bytes > 0)
{
  //读取串口数据
  //ch = Serial.read();
  ch = blueTeeth.read();
  if (-1 == ch)
{
  Serial.println("read fail");
}
  else
  {
    //打印出开发板接收到电脑发送的串口数据
    Serial.println(ch);
    car_ctrl(ch);
    //led_on(LED_PIN);
    //delay(200);                                                                                                                                          
   // led_off(LED_PIN);
    }
}
}
